<template>
    <div class="article-lists">
        <el-card class="!border-none mt-4" shadow="never">
            <e-space>
                <el-button @click="handleAdd" type="primary" class="mb-4">
                    <template #icon>
                        <icon name="el-icon-Plus" />
                    </template>
                    新增品牌
                </el-button>
            </e-space>
            <el-table size="large" v-loading="pager.loading" :data="pager.lists">
                <el-table-column label="品牌名称" prop="name" min-width="80" />

                <el-table-column label="操作" width="120" fixed="right">
                    <template #default="{ row }">
                        <e-space>
                            <el-button type="primary" link @click="handleEdit(row)">
                                编辑
                            </el-button>
                            <el-button type="primary" link @click="handleDelete(row.id)">
                                删除
                            </el-button>
                        </e-space>
                    </template>
                </el-table-column>
            </el-table>
            <div class="flex justify-end mt-4">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
        <edit-popup v-if="showEdit" ref="editRef" @success="getLists" @close="showEdit = false" />
    </div>
</template>
<script lang="ts" setup name="articleLists">
import { usePaging } from '@/hooks/usePaging'
import feedback from '@/utils/feedback'
import EditPopup from './edit.vue'
import { listPage1, del1, edit1 } from '@/servers/goods/api/supplyChainshangpinpinpai'
const queryParams = reactive({
    title: '',
    cid: '',
    isShow: ''
})

const { pager, getLists, resetPage, resetParams } = usePaging({
    fetchFun: listPage1,
    params: queryParams
})

const showEdit = ref(false)
const editRef = shallowRef<InstanceType<typeof EditPopup>>()

const handleAdd = async () => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('add')
}

const handleEdit = async (data: any) => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('edit', data.id)
    editRef.value?.setFormData(data)
}

const handleDelete = async (id: number) => {
    await feedback.confirm('确定要删除？')
    await del1({ id })
    feedback.msgSuccess('删除成功')
    getLists()
}

onActivated(() => {
    getLists()
})

getLists()
</script>

<style lang="scss" scoped>
.el-dropdown-link {
    display: flex;
    align-items: center;
}
</style>
